package com.hcbd.tdwt.ui.xingyu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;


import com.hcbd.tdwt.R;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * - @Description:  意见反馈图片列表
 * - @Author:  LXJ
 * - @Time:  2019/8/27 13:55
 */
public class FeedbackPictureListAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private OnItemClickListener mOnItemClickListener;

    public FeedbackPictureListAdapter(Context context, List<String> mList, OnItemClickListener onItemClickListener) {
        this.mContext = context;
        this.mList = mList;
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.picture_list_item, null);
            holder = new ViewHolder();
            holder.itemLayout = convertView.findViewById(R.id.item_layout);
            holder.pictureLayout = convertView.findViewById(R.id.picture_layout);
            holder.itemLayout = convertView.findViewById(R.id.item_layout);
            holder.pictureImg = convertView.findViewById(R.id.picture_img);
            holder.deleteBtn = convertView.findViewById(R.id.delete_btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            Glide.with(mContext).load(R.drawable.add_picture_icon)
                    .crossFade()
                    .into(holder.pictureImg);
            holder.deleteBtn.setVisibility(View.GONE);
            holder.pictureImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItemClickListener(v, position);
                }
            });
            holder.pictureImg.setClickable(true);
            holder.deleteBtn.setClickable(false);
        } else {
            holder.deleteBtn.setVisibility(View.VISIBLE);
            //图片地址
            String url = mList.get(position);
            //加载图片
            Glide.with(mContext).load(url)
                    .error(R.drawable.default_icon)
                    .crossFade()
                    .into(holder.pictureImg);
            //删除按钮
            holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.OnItemClickListener(v, position);
                }
            });
            holder.deleteBtn.setClickable(true);
            holder.pictureImg.setClickable(false);
        }
        return convertView;
    }

    /**
     * 点击事件
     */
    public interface OnItemClickListener {
        //点击事件
        void OnItemClickListener(View view, int postion);
    }

    public class ViewHolder {
        //子项布局
        private RelativeLayout itemLayout;
        //图片视图
        private RelativeLayout pictureLayout;
        //图片
        ImageView pictureImg;
        //图片删除按钮
        ImageView deleteBtn;
    }
}
